﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HZ.Supervise.Web.Meet
{
    public partial class CHRYList : BasePage
    {
        IBLL.IBLL_ProcureCatalog ihc = new HZ.Supervise.BLL.BLL_ProcureCatalog();
        IBLL.IBLL_User log = BLLFactory.BLL_User(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["mid"] != null)
                {
                    IsPxContent();
                    BindList();
                }
                else
                {
                    Response.Redirect("MeetList.aspx");
                }
            } 
        }
        private void IsPxContent()
        {
            int px = ihc.IsPxContent(CurrentAccount.GetUser().Org.OrgID, Request["mid"].ToString());
            if (px > 0)
            {
                btnXu.Text = "查看培训内容需求";
            }
            else
            {
                btnXu.Text = "填写培训内容需求";
            }
        }
        //判断报名是否过时 1过时 0未过时
        private int BMGS(string mid)
        {
            return ihc.IsBmGuoShi(mid);
        }
        private void BindList()
        {
            int totalCount = 0;
            Model.User u = CurrentAccount.GetUser();
            string str = string.Format(" and JdjgID='{0}' and CHAreaID='{1}' and MID='{2}'", u.Org.OrgID, u.Org.Area.AreaID, Request["mid"].ToString());
            if (!string.IsNullOrEmpty(txtHName.Text.Trim()))
            {
                str += string.Format(" and HosName like '%{0}%'", txtHName.Text.Trim());
            }
            DataTable dt = ihc.GetMeetInfoList(str, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvw.PageSize = AspNetPager1.PageSize;
            gvw.DataSource = dt;
            gvw.DataBind();
            if (gvw.Rows.Count == 0)
            {
                btnJian.Visible = false;
            }
            if (BMGS(Request["mid"].ToString()) == 1)//报名截止
            {
                btnAdd.Visible = false;
                btnJian.Visible = false;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddHos.aspx?mid=" + Request["mid"].ToString());
        }

        //删减人员
        protected void btnJian_Click(object sender, EventArgs e)
        {
            if (BMGS(Request["mid"].ToString()) == 1)//报名截止
            {
                HZ.Web.WebUtil.Alert("会议报名时间已截止，操作失败！", this);
                return;
            }
            //判断用户是否勾选
            if (Request["checkbox1"] == null)
            {
                HZ.Web.WebUtil.Alert("请至少勾选一项！", this);
            }
            else
            {
                string chkid = Request.Form["checkbox1"].ToString();
                string[] items = chkid.Split(',');
                string ids = "";
                for (int i = 0; i < items.Length; i++)
                {
                    ids += "'" + items[i] + "',";
                }
                int flag = ihc.DelCHRYInfo(ids.Trim(','));
                if (flag == 1)
                {
                    HZ.Supervise.Model.KeyOperationLogJG keyOperationLog = new HZ.Supervise.Model.KeyOperationLogJG();
                    keyOperationLog.OperationLogSerialID = Guid.NewGuid().ToString();
                    keyOperationLog.Operator = CurrentAccount.GetUser().UserID;
                    keyOperationLog.OrgID = CurrentAccount.GetUser().Org.OrgID;
                    keyOperationLog.OperateTime = DateTime.Now;
                    keyOperationLog.OperateMessage = string.Format("删除参会人员信息[{0}]", chkid);
                    log.AddKeyOperationLog(keyOperationLog);
                    HZ.Web.WebUtil.Alert("删除成功！");
                }
                else
                {
                    HZ.Web.WebUtil.Alert("删除失败！");
                }
                BindList();
            }
        }

        protected void gvw_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (BMGS(Request["mid"].ToString()) == 1)//报名截止
            {
                HZ.Web.WebUtil.Alert("会议报名时间已截止，操作失败！", this); 
                return;
            }
            long a = 0;
            int rowIndex = e.RowIndex;
            string yid = this.gvw.DataKeys[rowIndex].Values[0].ToString();
            string txtName = ((TextBox)this.gvw.Rows[rowIndex].FindControl("txtName")).Text.Trim();
            string txtXB = ((TextBox)this.gvw.Rows[rowIndex].FindControl("txtXB")).Text.Trim();
            string txtPostion = ((TextBox)this.gvw.Rows[rowIndex].FindControl("txtPostion")).Text.Trim();
            string txtTelNo = ((TextBox)this.gvw.Rows[rowIndex].FindControl("txtTelNo")).Text.Trim();
            string txtIsZS = ((TextBox)this.gvw.Rows[rowIndex].FindControl("txtIsZS")).Text.Trim();

            if (string.IsNullOrEmpty(txtName) || string.IsNullOrEmpty(txtXB) || string.IsNullOrEmpty(txtPostion) || string.IsNullOrEmpty(txtTelNo) || string.IsNullOrEmpty(txtIsZS))
            {
                HZ.Web.WebUtil.Alert("参会人员信息不能为空！", this);
                return;
            }
            if (txtXB != "男" && txtXB != "女")
            {
                HZ.Web.WebUtil.Alert("性别请填男或女！", this);
                return;
            }
            if (txtIsZS != "是" && txtIsZS != "否")
            {
                HZ.Web.WebUtil.Alert("住宿请填是或否！", this);
                return;
            }
            if (!Int64.TryParse(txtTelNo, out a))
            {
                HZ.Web.WebUtil.Alert("请确保联系电话无误！", this);
                return;
            }

            HZ.Supervise.Model.MeetInfo mt = new HZ.Supervise.Model.MeetInfo();
            mt.ID = yid;
            mt.Name = txtName;
            mt.XB = txtXB;
            mt.Postion = txtPostion;
            mt.TelNo = txtTelNo;
            mt.IsZS = txtIsZS;
            if (ihc.UpdateCHRYInfo(mt) == 1)
            {
                HZ.Supervise.Model.KeyOperationLogJG keyOperationLog = new HZ.Supervise.Model.KeyOperationLogJG();
                keyOperationLog.OperationLogSerialID = Guid.NewGuid().ToString();
                keyOperationLog.Operator = CurrentAccount.GetUser().UserID;
                keyOperationLog.OrgID = CurrentAccount.GetUser().Org.OrgID;
                keyOperationLog.OperateTime = DateTime.Now;
                keyOperationLog.OperateMessage = string.Format("监管修改参会人员信息[{0}]", mt.ID);
                log.AddKeyOperationLog(keyOperationLog);
            }
            else
            {
                HZ.Web.WebUtil.Alert("操作失败！", this);
            }
            this.gvw.EditIndex = -1;
            this.BindList();
        }

        protected void gvw_RowEditing(object sender, GridViewEditEventArgs e)
        {
            if (BMGS(Request["mid"].ToString()) == 1)//报名截止
            {
                HZ.Web.WebUtil.Alert("会议报名时间已截止，操作失败！", this);
                return;
            }
            this.gvw.EditIndex = e.NewEditIndex;
            this.BindList();
        }

        protected void gvw_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.gvw.EditIndex = -1;
            this.BindList();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindList();
        }

        protected void btnexcel_Click(object sender, EventArgs e)
        {
            int totalCount = 0;
            Model.User u = CurrentAccount.GetUser();
            string str = string.Format(" and JdjgID='{0}' and CHAreaID='{1}' and MID='{2}'", u.Org.OrgID, u.Org.Area.AreaID, Request["mid"].ToString());
            if (!string.IsNullOrEmpty(txtHName.Text.Trim()))
            {
                str += string.Format(" and HosName like '%{0}%'", txtHName.Text.Trim());
            }
            DataTable dt = ihc.GetMeetInfoList(str, 1, 0, out totalCount);
            if (dt.Rows.Count > 0)
            {
                ExportFile.Instance.ExportData(dt, gvw, this);
            }
        }

        protected void btnXu_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddPxContent.aspx?mid=" + Request["mid"].ToString());
        }
    }
}